package com.marsproj.bankingmanagesystem.controller;

import com.alibaba.fastjson.JSON;
import com.marsproj.bankingmanagesystem.entity.Account;
import com.marsproj.bankingmanagesystem.entity.Flow;
import com.marsproj.bankingmanagesystem.tools.LockBase;
import com.marsproj.bankingmanagesystem.service.AccountService;
import com.marsproj.bankingmanagesystem.service.FlowService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

@Api(tags = "账户的API")
@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private FlowService flowService;


    @ApiOperation("添加账户")
    @ApiImplicitParam(name = "account", value = "账户信息", required = true)
    @ApiResponse(code = 400, message = "前端传输参数格式错误")
    @RequestMapping(value = "/addAccount", method = RequestMethod.POST)
    public String addAccount(@RequestBody Account account) {
        accountService.addAccount(account);
        return "ok";
    }

    @ApiOperation("交易")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "domid", value = "主体id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "subid", value = "客体id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "转入为1，转出为0", required = true, dataType = "int"),
            @ApiImplicitParam(name = "temp", value = "交易额", required = true, dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, dataType = "String")
    })
    @ApiResponse(code = 400, message = "前端传输参数格式错误")
    @RequestMapping(value = "/deal", method = RequestMethod.POST)
    public String handleDeal(String domid, String subid, Integer type, String temp, String remark) {
        LockBase lockBase = LockBase.getLockBase(domid, subid);
        synchronized (lockBase) {
            BigDecimal change = new BigDecimal(temp);
            BigDecimal domMoney = new BigDecimal(queryOne(domid));
            BigDecimal subMoney = new BigDecimal(queryOne(subid));
            if (type > 0) {
                subMoney = subMoney.subtract(change);
                if (subMoney.compareTo(BigDecimal.ZERO) < 0) return "error! sub count don't have enough money";
                domMoney = domMoney.add(change);
            } else {
                domMoney = domMoney.subtract(change);
                if (domMoney.compareTo(BigDecimal.ZERO) < 0) return "error! dom count don't have enough money";
                subMoney = subMoney.add(change);
            }
            accountService.handleDeal(domMoney, domid);
            accountService.handleDeal(subMoney, subid);

            Date date = new Date();
            flowService.addFlow(new Flow(domid, subid, type, temp, date, remark));
            flowService.addFlow(new Flow(subid, domid, type ^ 1, temp, date, remark));

        }
        return "ok";
    }

    @ApiOperation("查询某个账户的余额")
    @ApiImplicitParam(name = "uid", value = "账号", required = true, dataType = "String")
    @ApiResponse(code = 400, message = "前端传输参数格式错误")
    @RequestMapping(value = "/query/{uid}", method = RequestMethod.GET)
    public String queryOne(@PathVariable String uid) {
        return accountService.queryOne(uid).toString();
    }

    @ApiOperation("查询全部账户的余额")
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public String queryAll() {
        return JSON.toJSONString(accountService.queryAll());
    }

    @ApiOperation("查询某个账户的全部交易流水")
    @ApiImplicitParam(name = "id", value = "账号", required = true, dataType = "String")
    @ApiResponse(code = 400, message = "前端传输参数格式错误")
    @RequestMapping(value = "/queryFlow/{id}", method = RequestMethod.GET)
    public String queryFlow(@PathVariable String id) {
        System.out.println(flowService.queryFlow(id));
        return JSON.toJSONString(flowService.queryFlow(id));
    }

}
